package kr.ac.jejuuniv.repository;

import java.util.List;

import kr.ac.jejuuniv.model.User;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
//@Repository
public class HibernateUserRepositoryImpl implements UserRepository {
	@Autowired
	private HibernateTemplate hibernateTemplate; 
	
	public List<User> findAll() {
		return hibernateTemplate.find("from User");
	}

	public void insert(User user) {
		hibernateTemplate.save(user);

	}

	public User findById(String id) {
		List<User> userList =  (List<User>)hibernateTemplate.find("from User where id=?",id);
		if (!CollectionUtils.isEmpty(userList)) {
			return userList.get(0);
		} else {
			return null;
		}
	}

	public void update(User user) {
		hibernateTemplate.update(user);

	}

	public void delete(String id) {
		hibernateTemplate.delete(new User(id,"",""));
	}

}
